//
//  VisitingDetailViewController.m
//  JinChengYun
//
//  Created by 金橙云 on 2017/3/1.
//  Copyright © 2017年 金橙云. All rights reserved.
//

#import "VisitingDetailViewController.h"

#import "XiuVisitViewController.h"
#import "DouSecondCell.h"
#import "DouDetailsCell.h"
#import "TodoHttpManager.h"

@interface VisitingDetailViewController ()<UITableViewDelegate,UITableViewDataSource>

{
    NSArray *_tiArr;
    NSMutableDictionary *_visitXDic;
    NSString *str;
    NSString *str1;
    NSString *str2;
    NSString *str3;
    NSString *str4;
    NSString *str5;
    NSString *str6;
    NSString *str7;
    NSString *str8;
    NSString *str9;
    NSString *str10;
    NSString *str11;
    NSString *str12;
    NSString *str13;
    NSString *str14;
    NSString *str15;
    NSString *str16;
    NSString *str17;
    NSString *str18;
    NSString *str19;
    NSString *str20;
    NSString *str21;
    NSString *str22;
    NSString *str23;
    NSString *strNum;
    
    NSMutableArray *_backArr;
    NSDictionary *dataDic;
}
@property(nonatomic,strong)UITableView *tabView;
@property (nonatomic,assign) CGFloat kangYinHeight;
@property (nonatomic,assign) CGFloat kangYinTextHeight;
@end

@implementation VisitingDetailViewController
-(void)viewWillAppear:(BOOL)animated
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didAATDGuKeLaiFX:) name:TDGuKeLaiFX object:nil];

    [_manager UserGMKeLaiFangXWithID:_visitID]; 
  self.tabBarController.tabBar.hidden  = YES;
    
}

-(void)didAATDGuKeLaiFX:(NSNotification *)notidi
{
    if (notidi.object != nil)
    {
        int status = [[notidi.object objectForKey:@"status"] intValue];
        dataDic = [notidi.object objectForKey:@"data"];
        if (status == 1)
        {
            _visitXDic = [notidi.object objectForKey:@"data"];
            str = [_visitXDic objectForKey:@"visit_time"];//来访日期
            NSLog(@"%@",str);
            str1 = [_visitXDic objectForKey:@"name"];//客户姓名
            str2 = [_visitXDic objectForKey:@"sex"];//性别
            strNum = [_visitXDic objectForKey:@"phone"];

            str3 = [_visitXDic objectForKey:@"id_card"];
            str4 = [_visitXDic objectForKey:@"id_card_dz"];
            str5 = [_visitXDic objectForKey:@"khdj"];
            str6 = [_visitXDic objectForKey:@"age_group"];
            str8 = [_visitXDic objectForKey:@"remark"];
            str9 = [_visitXDic objectForKey:@"ggtg"];
            str10 = [_visitXDic objectForKey:@"tjqd"];
            
            str11 = [_visitXDic objectForKey:@"tjfs"];
            str12 = [_visitXDic objectForKey:@"recommend_name"];
            str13 = [_visitXDic objectForKey:@"recommend_phone"];
            str14 = [_visitXDic objectForKey:@"live_area"];
            str15 = [_visitXDic objectForKey:@"work_area"];
            str16 = [_visitXDic objectForKey:@"job"];
            
            str17 = [_visitXDic objectForKey:@"vehicle"];
            str18 = [_visitXDic objectForKey:@"zymd"];
            str19 = [_visitXDic objectForKey:@"need_property"];
            str20 = [_visitXDic objectForKey:@"order_area"];
            str21 = [_visitXDic objectForKey:@"order_type"];
            str22 = [_visitXDic objectForKey:@"doubt"];
            str23 = [_visitXDic objectForKey:@"remind_time"];
            if (str22.length>0) {
                NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:14]};  //指定字号
                CGRect rect = [str22 boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width-12-114, MAXFLOAT)/*计算宽度时要确定高度*/ options:NSStringDrawingUsesLineFragmentOrigin |
                               NSStringDrawingUsesFontLeading attributes:dic context:nil];
                _kangYinTextHeight = rect.size.height+12;
                if (_kangYinTextHeight<20) {
                    _kangYinTextHeight = 20;
                }
                _kangYinHeight = _kangYinTextHeight +12*2;
            }else{
                _kangYinTextHeight = 20;
                _kangYinHeight = 44;
            }
        
            _backArr = [NSMutableArray arrayWithObjects:str,str1,str2,strNum,str3,str4,str5,str6,str8,str9,str10,str11,str12,str13,str14,str15,str16,str17,str18,str19,str20,str21,str22,str23, nil];
            
            [self.tabView reloadData];
            
            
            
        }
    }
    
}
-(void)viewDidDisappear:(BOOL)animated
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:TDGuKeLaiFX object:nil];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"来访跟进";
    self.view.backgroundColor = [UIColor whiteColor];
    
    

    _tiArr = [NSArray arrayWithObjects:@"首访日期 :",@"客户姓名 :", @"客户性别 :",@"联系方式 :",@"身份证号码 :",@"身份证地址 :",@"客户等级 :",@"年龄阶段 :",@"客户白描 :",@"广告推广 :",@"推广渠道 :",@"推介方式 :",@"推荐人姓名 :",@"推荐人电话 :",@"居住区域 :",@"工作区域:",@"职业身份 :",@"交通工具 :",@"置业目的 :",@"需求物业 :",@"需求面积 :",@"需求户型 :",@"抗性因素 :",@"下次跟进日期 :",nil];

    _visitXDic = [NSMutableDictionary dictionaryWithCapacity:0];
    _manager = [TodoHttpManager sharedManager];
    _tabView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, B_WIDTH, B_HEIGHT) style:UITableViewStyleGrouped];
    _tabView.delegate = self;
    _tabView.dataSource = self;
    _tabView.rowHeight = 44;
    [self.view addSubview:_tabView];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _tiArr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIndent = @"";
    if (indexPath.row == 3)
    {
        cellIndent = @"CellA";
        DouSecondCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        if (cell ==nil)
        {
            cell = [[DouSecondCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndent];
            
            strNum =  [_visitXDic objectForKey:@"phone"];
            cell.secondLab.text = [NSString stringWithFormat:@"%@",strNum.length>0?strNum:@""];
            [cell setUpUIPbone:[_visitXDic objectForKey:@"phone"]];
            
        }
        
        cell.firstLab.text= @"联系电话:";
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];//取消点击
        return cell;
        
    }
    else
    {
        cellIndent = @"CellB";
        DouDetailsCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        if (cell ==nil)
        {
            cell = [[DouDetailsCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndent];
            
            
        }
        if ([cell isKindOfClass:[DouDetailsCell class]])
        {
            if (_backArr.count != 0)
            {
                
                NSString *str0 = (NSString *)_backArr[indexPath.row];
                cell.secondLab.text = [NSString stringWithFormat:@"%@",str0];
                // NSLog(@"jiciya-----%@",_daArr);
            }
            
        }
        if (indexPath.row==22) {
            cell.rightHeight = _kangYinTextHeight;
            cell.rightStr = str22;
        }
        cell.firstLab.text = _tiArr[indexPath.row];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];//取消点击
        return cell;
        
    }
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row==22) {
        return _kangYinHeight-8>44?_kangYinHeight-8:44;
    }
    return 44;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return nil;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] init];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame= CGRectMake(20, 25,B_WIDTH-20*2, 45);
    btn.layer.cornerRadius = 6;
    btn.backgroundColor = [UIColor orangeColor];
    [btn setTitle:@"修改" forState:0];
    [btn setTitleColor:[UIColor whiteColor] forState:0];
    [btn addTarget:self action:@selector(visitXiuGaiBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:btn];
    
    [self.view addSubview:view];
    return view;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 100;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 2;
}




-(void)visitXiuGaiBtnClick:(UIButton *)btn
{
    XiuVisitViewController *xiuVisitiVC = [[XiuVisitViewController alloc] init];
    xiuVisitiVC.xLaiFangStr = _visitID;
    xiuVisitiVC.nameStr = str1;
    xiuVisitiVC.nextPhone = strNum;
    xiuVisitiVC.visitChDic = _visitXDic;
    //NSLog(@"0000000------电机的%@",_visitID);
    xiuVisitiVC.CustomID = [_visitXDic objectForKey:@"customer_id"];
    xiuVisitiVC.dataDic = dataDic;
    if (self.isFirst==YES) {
        
    }else{
        xiuVisitiVC.isSecond = YES;
/*
 
 */
    }
    [self.navigationController pushViewController:xiuVisitiVC animated:NO];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
